package com.njsoft.huanxin


import android.app.ProgressDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.hyphenate.EMError
import com.hyphenate.chat.EMClient
import com.hyphenate.exceptions.HyphenateException
import kotlinx.android.synthetic.main.activity_register.*


/**
 * A login screen that offers login via email/password.
 */
class RegisterActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        initListener()
    }

    fun initListener() {
        Register.setOnClickListener {
            val mUserName = UserName.text.toString().trim()
            val mPassword = Password.text.toString().trim()
            if (!TextUtils.isEmpty(mUserName) && !TextUtils.isEmpty(mPassword)) {
                val pd = ProgressDialog(this)
                pd.setMessage("注册中..")
                pd.show()
                Thread() {
                    run {
                        try {
                            EMClient.getInstance().createAccount(mUserName, mPassword);//同步方法
                            runOnUiThread {
                                if (!this@RegisterActivity.isFinishing)
                                    pd.dismiss()
                                    Toast.makeText(applicationContext, "注册成功", Toast.LENGTH_SHORT).show()
                                    finish()
                            }
                        } catch (e: HyphenateException) {
                            runOnUiThread {
                                if (!this@RegisterActivity.isFinishing)
                                    pd.dismiss()
                                val errorCode = e.getErrorCode()
                                if (errorCode == EMError.NETWORK_ERROR) {
                                    Toast.makeText(applicationContext,"未联网", Toast.LENGTH_SHORT).show()
                                } else if (errorCode == EMError.USER_ALREADY_EXIST) {
                                    Toast.makeText(applicationContext, "已经注册过了", Toast.LENGTH_SHORT).show()
                                } else if (errorCode == EMError.USER_AUTHENTICATION_FAILED) {
                                    Toast.makeText(applicationContext, "注册失败，未开发权限", Toast.LENGTH_SHORT).show()
                                } else if (errorCode == EMError.USER_ILLEGAL_ARGUMENT) {
                                    Toast.makeText(applicationContext,"用户名不合法", Toast.LENGTH_SHORT).show()
                                } else if (errorCode == EMError.EXCEED_SERVICE_LIMIT) {
                                    Toast.makeText(this@RegisterActivity, "您已经超出了社区版的限制，请升级到企业版", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(applicationContext, "注册失败", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }.start()

            }
        }
    }


}
